package se_dsp_adv_plan

import (
	"fmt"
	"strconv"
	"strings"
	"time"
	"wx-api/dsp-api/cache"
	"wx-api/dsp-api/errmsg"
	"wx-api/dsp-api/handlers"
	"wx-api/dsp-api/model/m_dsp_adv_plan"
	"wx-api/dsp-api/services/comm"
	"wx-api/dsp-api/services/se_dsp_report"
	"wx-api/dsp-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_plan.List(id, name, "", flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	tmplist := []m_dsp_adv_plan.DspAdvPlan{}
	ttt := data.Data.(*[]m_dsp_adv_plan.DspAdvPlan)
	for _, v := range *ttt {
		tmp := v
		sinfo, ok := cache.CacheDspAdvertiserMapNGet(v.AdvId)
		if ok {
			tmp.AdvName = sinfo.Name
		} else {
			tmp.AdvName = "-"
		}
		if len(v.PMedia) > 0 {
			for i, media := range v.PMedia {
				minfo, ok := cache.CacheDspMediaMapNGet(media.MediaId)
				if ok {
					media.MediaName = minfo.Name
				} else {
					media.MediaName = "-"
				}
				mainfo, ok := cache.CacheDspMediaAppMapNGet(media.AppId)
				if ok {
					media.AppName = mainfo.Name
				} else {
					media.AppName = "-"
				}
				msinfo, ok := cache.CacheDspMediaSlotMapNGet(media.SlotId)
				if ok {
					media.SlotName = msinfo.Name
				} else {
					media.SlotName = "-"
				}
				v.PMedia[i] = media
			}
		}
		//消耗
		tda := se_dsp_report.GetAutoDataCache("plan-t-" + v.Id.Hex())
		yda := se_dsp_report.GetAutoDataCache("plan-y-" + v.Id.Hex())
		tmp.TCost = utils.Decimal(tda.Price / 100000)
		tmp.YCost = utils.Decimal(yda.Price / 100000)
		tmplist = append(tmplist, tmp)
	}
	data.Data = tmplist
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_dsp_adv_plan.DspAdvPlan
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	if form.PmediaRadio != "首选媒体" {
		form.PMedia = nil
	}
	if form.PstaRadio != "设置开始和结束日期" {
		form.PSta = ""
		form.PEnd = ""
	}
	form.Prty = 1
	if form.PtmsRadio != "指定时段" {
		form.PTms = m_dsp_adv_plan.DspAdvPlanPtms{
			One: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Two: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			The: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Fon: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Fiv: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Sey: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Sun: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
		}
	}
	form.Code = strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
	id, err := m_dsp_adv_plan.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheDspAdvPlanMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_dsp_adv_plan.DspAdvPlan
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	adxinfo, err := m_dsp_adv_plan.GetByCk(form.Id.Hex())
	if err != nil || adxinfo == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	if form.PmediaRadio != "首选媒体" {
		form.PMedia = nil
	}
	if form.PstaRadio != "设置开始和结束日期" {
		form.PSta = ""
		form.PEnd = ""
	}
	form.Prty = 1
	if form.PtmsRadio != "指定时段" {
		form.PTms = m_dsp_adv_plan.DspAdvPlanPtms{
			One: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Two: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			The: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Fon: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Fiv: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Sey: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
			Sun: []bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
		}
	}
	err = m_dsp_adv_plan.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheDspAdvPlanMapUpdate(adxinfo.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_plan.GetByCk(id)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_plan.GetByCk(id)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_dsp_adv_plan.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheDspAdvPlanMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_plan.GetByCk(id)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_dsp_adv_plan.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheDspAdvPlanMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func All(c *handlers.Controller) {
	id := c.GetString("ids")
	ids := strings.Split(id, ",")
	ids = utils.UnqArrs(ids)
	data := cache.CacheDspAdvPlanMapAll(ids)
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}
